#include "prompteditor.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLineEdit>
#include <QTextEdit>
#include <QPushButton>
#include <QLabel>
#include <QMessageBox>
#include <QDebug>
#include <QCoreApplication>

PromptEditor::PromptEditor(QWidget *parent)
    : QDialog(parent)
    , m_translatePrompt(TranslatePrompt::getInstance())
{
    m_jsonPath = QCoreApplication::applicationDirPath() + "/translateprompt.json";
    initUI();
    initConnections();
    loadPrompt();
}

PromptEditor::~PromptEditor()
{
}

void PromptEditor::initUI()
{
    QVBoxLayout* mainLayout = new QVBoxLayout(this);

    // 提示语编辑
    QLabel* promptLabel = new QLabel("翻译提示:", this);
    m_promptEdit = new QLineEdit(this);
    mainLayout->addWidget(promptLabel);
    mainLayout->addWidget(m_promptEdit);

    // 前缀编辑
    QLabel* prefixLabel = new QLabel("聊天前缀:", this);
    m_prefixEdit = new QLineEdit(this);
    mainLayout->addWidget(prefixLabel);
    mainLayout->addWidget(m_prefixEdit);

    // 后缀编辑
    QLabel* suffixLabel = new QLabel("聊天后缀:", this);
    m_suffixEdit = new QLineEdit(this);
    mainLayout->addWidget(suffixLabel);
    mainLayout->addWidget(m_suffixEdit);

    // 用户提示列表编辑
    QLabel* userPromptsLabel = new QLabel("用户提示列表(每行一个):", this);
    m_userPromptsEdit = new QTextEdit(this);
    mainLayout->addWidget(userPromptsLabel);
    mainLayout->addWidget(m_userPromptsEdit);

    // 助手提示列表编辑
    QLabel* assistantPromptsLabel = new QLabel("助手提示列表(每行一个):", this);
    m_assistantPromptsEdit = new QTextEdit(this);
    mainLayout->addWidget(assistantPromptsLabel);
    mainLayout->addWidget(m_assistantPromptsEdit);

    // 按钮布局
    QHBoxLayout* buttonLayout = new QHBoxLayout();
    m_resetButton = new QPushButton("恢复默认", this);
    m_saveButton = new QPushButton("保存", this);
    m_cancelButton = new QPushButton("取消", this);
    buttonLayout->addWidget(m_resetButton);
    buttonLayout->addWidget(m_saveButton);
    buttonLayout->addWidget(m_cancelButton);
    mainLayout->addLayout(buttonLayout);

    setWindowTitle("编辑翻译提示");
    resize(400, 600);
}

void PromptEditor::initConnections()
{
    connect(m_saveButton, &QPushButton::clicked, this, &PromptEditor::savePrompt);
    connect(m_cancelButton, &QPushButton::clicked, this, &QDialog::reject);
    connect(m_resetButton, &QPushButton::clicked, this, &PromptEditor::resetPrompt);
}

void PromptEditor::updateDisplay(const PromptInfo& info)
{
    // 更新基本字段显示
    m_promptEdit->setText(info.prompt);
    m_prefixEdit->setText(info.chatPrefix);
    m_suffixEdit->setText(info.chatSuffix);

    // 更新用户提示列表显示
    QString userPrompts;
    for (const QString& prompt : info.userPromptList) {
        userPrompts += prompt + "\n";
    }
    m_userPromptsEdit->setText(userPrompts);

    // 更新助手提示列表显示
    QString assistantPrompts;
    for (const QString& prompt : info.assistantPromptList) {
        assistantPrompts += prompt + "\n";
    }
    m_assistantPromptsEdit->setText(assistantPrompts);
}

void PromptEditor::loadPrompt()
{
    // 加载现有配置
    m_translatePrompt->load(m_jsonPath);
    updateDisplay(m_translatePrompt->getPrompt());
}

void PromptEditor::savePrompt()
{
    // 获取编辑框中的内容
    QString prompt = m_promptEdit->text();
    QString prefix = m_prefixEdit->text();
    QString suffix = m_suffixEdit->text();

    // 将多行文本转换为列表
    QVector<QString> userPrompts;
    QStringList userLines = m_userPromptsEdit->toPlainText().split("\n");
    for (const QString& line : userLines) {
        if (!line.trimmed().isEmpty()) {
            userPrompts.append(line.trimmed());
        }
    }

    QVector<QString> assistantPrompts;
    QStringList assistantLines = m_assistantPromptsEdit->toPlainText().split("\n");
    for (const QString& line : assistantLines) {
        if (!line.trimmed().isEmpty()) {
            assistantPrompts.append(line.trimmed());
        }
    }

    // 更新提示信息
    m_translatePrompt->changePrompt(prompt, prefix, suffix, userPrompts, assistantPrompts);

    // 保存到文件
    if (m_translatePrompt->save(m_jsonPath)) {
        QMessageBox::information(this, "成功", "提示信息已保存");
        accept();
    } else {
        QMessageBox::warning(this, "错误", "保存提示信息失败");
    }
}

void PromptEditor::resetPrompt()
{
    if (QMessageBox::question(this, "确认", "确定要恢复默认提示吗？",
                            QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes) {
        m_translatePrompt->resetToDefault();
        updateDisplay(m_translatePrompt->getPrompt());  // 使用新函数更新显示
        QMessageBox::information(this, "成功", "已恢复默认提示");
    }
} 